const week = {         
    "0" : "日",         
    "1" : "一",         
    "2" : "二",         
    "3" : "三",         
    "4" : "四",         
    "5" : "五",         
    "6" : "六"        
};
/**
 * 设置边界
 * @param {*} item   获取边界
 * @param {*} M_H    model的高度
 * @param {*} scroll 滚动条的距离
 * @param {*} headerHeight  头部的高度
 * @param {*} left   全天model的left
 * @param {*} type   类型
 */
export const checkBorder = (item, M_H, scroll, headerHeight, leftModel, type) => {
    const {clientWidth, scrollHeight } = document.querySelector('.ssal-calendar')
    // 获取的target是距离祖先的距离, 不是距离page的距离
    // x 是当前点击的位置距离左边的距离, y 同理
    const { offsetTop, offsetLeft, offsetWidth, offsetHeight } = item.target ? item.target : item
    const { y, x } = item // 点击的位置
    const row = item.target ? (item.target.dataset.row || null) : item.dataset.row
    const multipleRow = item.target ? (item.target.dataset.multiplerow || null) : item.dataset.multiplerow // 多周视图row
    const M_W = 300 // model的宽度
    const Item_Type = item.target ? item.target.dataset.type : item.dataset.type
    const Day = item.target ? item.target.dataset.day : item.dataset.day
    let xLease = clientWidth - offsetLeft // x轴的剩余宽度
    let yLease = 0 // y轴的剩余高度
    let top = null
    let left = `${offsetLeft}px`
    let margin = ''
    let right = 'unset'
    let bottom = `unset`
    let multipleScroll = 0
    if(Item_Type == 'multipleEvent'){
        multipleScroll = document.querySelectorAll('.multiple-list')[0].clientHeight
    }
    if(leftModel || leftModel == 0){
        xLease = clientWidth - leftModel
    }
    if(row || row == 0) {
        if(y){
            yLease = scrollHeight - y
        }
        top = `calc(${Math.floor(100 / 6)}% * ${row} + ${offsetTop + 83}px)`
        margin = `30px 0 0 0`
        xLease -= offsetWidth
        if(Item_Type == 'monthMore'){
            // 月份更多事件
            left = `calc(100% / 7 * ${Day} + ${offsetLeft}px)`
            top = `calc(${Math.floor(100 / 6)}% * ${row} + ${offsetTop + 23}px)`
            xLease = clientWidth - x
        }
    }else{
        xLease = xLease - offsetWidth
        yLease = scrollHeight - offsetTop
        if(scroll || scroll == 0) {
            top = `${offsetTop - scroll + headerHeight}px`
        }else{
            if(leftModel || leftModel == 0){
                top = `${offsetTop + 22}px`
            }else{
                top = `${offsetTop + 61}px`
            }
        }
        if(leftModel || leftModel == 0) {
            left =  `${leftModel + offsetWidth}px`
        }else{
            left =  `${offsetLeft + offsetWidth + 82}px`
        }
        if(multipleRow || multipleRow == 0){
            if(multipleRow == 1 && Day > 3){
                top = `unset`
                bottom = `10px`
            }else{
                top = `calc(100% / 4 * ${multipleRow < 1 ? 0 : 2  - 0 + (Day < 4 ? 0 : 1)} + ${item.layerY + 56}px)`
            }
        }
    }
    if(xLease < M_W) {
        // x轴剩余宽度小于model宽度
        left = 'unset'
        if(row || row == 0) {
            right = `${xLease - 2}px`
        }else{
            right = `${xLease + offsetWidth - 98}px`
        }
        if(leftModel || leftModel == 0) {
            right = `${xLease}px`
        } 
        if((!row && row != 0) && offsetLeft < M_W){
            right = 'unset'
            left = `${offsetLeft + 82}px`
            if(offsetTop <= 0 && type != 'day'){
                top = `${offsetTop + 22 + 61}px`
            }
        }
    }else{
        if(leftModel || leftModel == 0) {
            left = `${leftModel + offsetWidth}px`
        }else if(row || row == 0){
            left = `${offsetLeft}px`
            if(Item_Type == 'monthMore'){
                left = `calc(100% / 7 * ${Day} + ${offsetLeft}px)`
            }
        }else{
            if(Item_Type == 'multipleEvent') {
                // 多周视图事件
                if(Day == 0 || Day - 4 == 0){
                    left = `10px`
                }else{
                    left = `calc(100% / 4 * ${Day > 3 ? Day - 3 : Day} - 30px)`
                }
            }else{
                left = `${offsetLeft + 82 + offsetWidth}px`
            }
        }
    }
    if(yLease < M_H) {
        // y轴剩余高度小于model高度
        top = 'unset'
        if(row || row == 0){
            if(Item_Type == 'monthMore'){
                bottom = `calc(${Math.floor(100 / 6)}% * ${6 - row} - ${offsetTop - 43}px)`
            }else{
                bottom = `calc(${Math.floor(100 / 6)}% * ${6 - row} - ${offsetTop + 35}px)`
            }
        }else{
            const topDistance = offsetTop - scroll + headerHeight + offsetHeight
            const bottomBorder = scrollHeight - topDistance - 60
            bottom = `${bottomBorder < 0 ? 0 : bottomBorder}px`
        }
    }
    return {
        top,
        left,
        margin,
        right,
        bottom
    }
}

/**
 * 切割时间
 * 
 */
export const cutTime = () => {
    let temp = []
    for(let i = 0; i < 1440; i += 30) {
        let m = i % 60
        let h = parseInt(i / 60);
        if(h < 10) {
            h = '0' + h
        }
        if(m < 10){
            m = '0' + m
        }
        temp.push({
            name: h + ':' + m,
            id: i
        })
    }
    return temp
}

/**
 * 生成ID
 */
export const radomID = () => {
    const random = Math.floor(Math.random() * 1000)
    const timeStr = new Date().getTime()
    return timeStr+random
}

/**
 * 指定上位的数
 */
export const mathNumber = (num, float) => {
    num = parseFloat(num).toFixed(1)
    num = String(num)
    const split = num.split('.')[1]
    // console.log(Number(split), float)
    if(Number(split) > float){
        return Math.ceil(num)
    }else{
        return Number(num.split('.')[0])
    }
}

// 获取当前时间
export const getCurrent = () => {
    const date = new Date()
    const year = date.getFullYear()
    let month = date.getMonth() + 1
    let day = date.getDate()
    let hour = date.getHours()
    let min = date.getMinutes()
    let dd = date.getDay()
    if(month < 10) month = '0' + month
    if(day < 10) day = '0' + day
    if(hour < 10) hour = '0' + hour
    if(min < 10) min = '0' + min
    return {
        year,
        month,
        day,
        hour,
        min,
        dd,
        fullDate: year + '-' + month + '-' + day
    }
}

// 格式化事件
export const format = (date, fmt) => {         
    var o = {         
        "M+" : date.getMonth() + 1, //月份         
        "d+" : date.getDate(), //日         
        "h+" : date.getHours()%12 == 0 ? 12 : date.getHours()%12, //小时         
        "H+" : date.getHours(), //小时         
        "m+" : date.getMinutes(), //分         
        "s+" : date.getSeconds(), //秒         
        "q+" : Math.floor((date.getMonth()+3)/3), //季度         
        "S" : date.getMilliseconds() //毫秒         
    };                
    if(/(y+)/.test(fmt)){         
        fmt = fmt.replace(RegExp.$1, (date.getFullYear()+"").substring(4 - RegExp.$1.length));         
    }         
    if(/(E+)/.test(fmt)){         
        fmt = fmt.replace(RegExp.$1, ((RegExp.$1.length>1) ? (RegExp.$1.length>2 ? "/u661f/u671f" : "/u5468") : "")+week[date.getDay()+""]);         
    }         
    for(var k in o){         
        if(new RegExp("("+ k +")").test(fmt)){         
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substring((""+ o[k]).length)));         
        }         
    }         
    return fmt;         
}

// 获取时区
export const getTimeZone = () => {
    const date = new Date()
    const timeZone = date.getTimezoneOffset() / -60
    if(timeZone >= 0){
        return timeZone < 10 ? '+0' + timeZone : '+' + timeZone
    }else{
        return Math.abs(timeZone) < 10 ? '-0' + Math.abs(timeZone) : Math.abs(timeZone)
    }
}


// 转换简短的时间显示  MM月DD日 周w & MM月DD日 - MM月DD日
export const formatShortTime = (date) => {
    // console.log(date)
    if(date.sameDay) {
       return date.startDate.substring(5,10).replace('-','月') + '日'  + ' 周' + week[date.startDay+""]
    }else{ 
        const start = date.actionStartTime
        const end = date.actionEndTime
        return start.substring(5,10).replace('-','月') + '日' + ' - ' + end.substring(5,10).replace('-','月') + '日'
    }
}